package com.ymatou.iapi.auth.testcase;

import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.auth.parameter.UserTokenDtoBean;
import com.ymatou.iapi.auth.service.UserTokenDtoCall;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymatou.iapi.userservice.parameter.req.CreateUserReq;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.database.mongo.UserTokenWapper;
import com.ymttest.database.sqlwapper.YmtUsersWapper;

/**
*创建获取token
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_UserTokenDto{
	private static UserTokenDtoBean usertokendtoBean;
	private static UserTokenDtoCall usertokendtoCall;
	private UserTokenWapper userTokenWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("创建获取token");
	}

	@Before
	public void caseUp() throws Exception {
		usertokendtoBean=new UserTokenDtoBean();
		usertokendtoCall=new UserTokenDtoCall();
		userTokenWapper=new UserTokenWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(usertokendtoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_UserTokenDto_001() {
		Logger.start(true,"用户存在token,获取token");
		try {
			UserRegisterInfoRequestDtoBean cur = new UserRegisterInfoRequestDtoBean();
			cur = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc =JUserCallService.UserRegisterInfoRequestDtoCall(cur); 
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			int UserId=registerResult.get("UserId").getAsInt();
			String AccessToken=registerResult.get("Token").getAsString();
			
			usertokendtoBean.setUserId(UserId);
//			usertokendtoBean.setTokenTimeOut(1);
			usertokendtoCall.setData(usertokendtoBean);
			usertokendtoCall.callService();	 
			
			Logger.verifyEquals(true, usertokendtoCall.getSuccess(), "验证Success");
			Logger.verifyEquals(200, usertokendtoCall.getCode(), "验证Code");
			Logger.verifyEquals(AccessToken, usertokendtoCall.getResult(), "验证Result");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Ts_UserTokenDto_002() {
		Logger.start(true,"用户不存在token,创建token");
		try {
			CreateUserReq cur=new CreateUserReq();
			cur=JUserCallService.initCreateUserBean(cur);
			int uid = JUserCallService.createUserCall(cur);
           
			usertokendtoBean.setUserId(uid);
			usertokendtoBean.setTokenTimeOut(1);
			usertokendtoCall.setData(usertokendtoBean);
			usertokendtoCall.callService();	 
			
			Logger.verifyEquals(true, usertokendtoCall.getSuccess(), "验证Success");
			Logger.verifyEquals(200, usertokendtoCall.getCode(), "验证Code");
			Thread.sleep(3000);
			Map<String, Object> userlst= userTokenWapper.getUserTokenByUid(uid);
			
			if(userlst!=null && userlst.size()>0)
				Logger.verifyEquals(userlst.get("sId"), usertokendtoCall.getResult(), "验证Result");		
			else
				Logger.verifyEquals("", usertokendtoCall.getResult(), "验证Result");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
//	@Test
//	@Category({P3.class})
//	@TestCase
//	public void Ts_UserTokenDto_003() {
//		Logger.start(false,"用户Id无效，返回失败");
//		try {
//			usertokendtoBean.setUserId(123321);
//			usertokendtoBean.setTokenTimeOut(1);
//			usertokendtoCall.setData(usertokendtoBean);
//			usertokendtoCall.callService();	 
//			
//			Logger.verifyEquals(false, usertokendtoCall.getSuccess(), "验证Success");
//			Logger.verifyEquals(404, usertokendtoCall.getCode(), "验证Code");
//			Logger.verifyEquals("", usertokendtoCall.getResult(), "验证Result");
//			
//		} catch (Exception e) {
//			Logger.fail(e);
//		}
//	}
}